package com.ss.android.image;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.bytedance.frameworks.baselib.network.http.util.d;
import com.ss.android.common.load.LRUWeakCache;
import com.ss.android.common.imagezoom.ImageViewTouch;
import com.ss.android.common.ui.view.ImageViewTouchViewPager;
import com.ss.android.image.loader.LargeImageLoader;
import com.ss.android.image.model.ImageInfo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import pl.droidsonroids.gif.GifDrawable;

public final class n extends Dialog implements Callback, d, LargeImageLoader.a {
    final Context mContext;
    final List g;
    final List h;
    final View.OnClickListener mOnClickListener;
    public LargeImageLoader b;
    public LRUWeakCache c;
    View mSaveTextView;
    ImageViewTouchViewPager mImagePager;
    FullPagerAdapter mFullPagerAdapter;
    int i;
    int j;
    private BaseImageManager mBaseImageManager;
    private TextView mPageNumber;
    private WeakHandler mWeakHandler;
    private boolean o;

    public n(Context arg3, BaseImageManager arg4) {
        super(arg3, android.R.style.Theme_NoTitleBar);
        this.mWeakHandler = new WeakHandler(((Callback) this));
        this.g = new ArrayList();
        this.h = new ArrayList();
        this.i = -1;
        this.j = -1;
        this.o = true;
        this.mOnClickListener = new o(this);
        this.mContext = arg3;
        this.mBaseImageManager = arg4;
        this.c = new LRUWeakCache();
    }

    static void a(View arg1, boolean arg2) {
        if (arg1 != null && arg1.isEnabled() != arg2) {
            arg1.setEnabled(arg2);
        }
    }

    public final void a(List arg2, int arg3) {
        this.g.clear();
        if (arg2 != null) {
            this.g.addAll(arg2);
        }

        this.i = arg3;
    }

    public final void a(String arg4) {
        Object v0 = ComponentsManifest.findComponent(com.ss.android.aa.class);
        if (v0 != null) {
            ((com.ss.android.aa) v0).a(this.mContext, "image", arg4);
        }
    }

    final void a() {
        if (this.mImagePager != null) {
            ImageInfo v0 = this.mFullPagerAdapter.b(this.mImagePager.getCurrentItem());
            if (v0 != null) {
                String v0_1 = v0.mUri;
                if (!android.support.design.a.isTextEmpty(v0_1)) {
                    this.mBaseImageManager.saveImage(this.mContext, com.bytedance.common.utility.aa.md5(v0_1), v0_1);
                }
            }
        }
    }

    final void a(int arg4) {
        if (this.mFullPagerAdapter != null && this.mPageNumber != null && (this.o)) {
            int v1 = this.mFullPagerAdapter.getCount();
            int v2 = arg4 + 1;
            String v0 = "";
            if (v2 > 0 && v2 <= v1 && v1 > 0) {
                v0 = v2 + "/" + v1;
            }

            this.mPageNumber.setText(((CharSequence) v0));
        }
    }

    public final void a(int arg3, Object arg4) {
        Message v0 = this.mWeakHandler.obtainMessage(257);
        v0.arg1 = arg3;
        v0.obj = arg4;
        this.mWeakHandler.sendMessage(v0);
    }

    public final void a(String arg2, Object arg3) {
        if ((this.isShowing()) && this.mFullPagerAdapter != null) {
            this.mFullPagerAdapter.a(arg2, arg3);
        }
    }

    public final void handleMsg(Message message) {
        Object v2 = null;
        int v3 = 0;
        if (message.what == 257 && this.mImagePager != null && this.mFullPagerAdapter != null) {
            int v1 = message.arg1;
            Object v4 = (message.obj instanceof String) ? message.obj : v2;
            if (v4 == null) {
                return;
            }

            if (!this.isShowing()) {
                return;
            }

            int v0 = v1 < 0 ? 0 : v1;
            v1 = v0 >= 100 ? 99 : v0;
            int v5 = this.mImagePager.getChildCount();
            while (v3 < v5) {
                Object v0_1 = this.mImagePager.getChildAt(v3).getTag();
                if (!(v0_1 instanceof b)) {
                    v0_1 = v2;
                }

                if (v0_1 != null && (((String) v4).equals(((b) v0_1).b))) {
                    ((b) v0_1).mProgress.setProgress(v1);
                    ((b) v0_1).mProgressText.setText(v1 + "%");
                }

                ++v3;
            }
        }
    }

    public final void onCreate(Bundle arg3) {
        super.onCreate(arg3);
        this.setContentView(com.ss.android.article.browser.R.layout.full_image_dlg);
        this.setCancelable(true);
        this.mSaveTextView = this.findViewById(com.ss.android.article.browser.R.id.save_textview);
        this.mPageNumber = this.findViewById(com.ss.android.article.browser.R.id.page_number);
        this.mImagePager = this.findViewById(com.ss.android.article.browser.R.id.image_pager);
        this.mFullPagerAdapter = new FullPagerAdapter(this, this.mContext);
        this.mImagePager.setOnPageChangeListener(new com.ss.android.image.p(this));
        this.mImagePager.setAdapter(this.mFullPagerAdapter);
        this.mSaveTextView.setOnClickListener(new q(this));
        this.setOnShowListener(new s(this));
    }

    protected final void onStop() {
        Bitmap v1 = null;
        super.onStop();
        if (this.mImagePager != null) {
            this.g.clear();
            this.mFullPagerAdapter.a(this.g);
            this.mFullPagerAdapter.notifyDataSetChanged();
            int v3 = this.mImagePager.getChildCount();
            int v2;
            for (v2 = 0; v2 < v3; ++v2) {
                Object v0 = this.mImagePager.getChildAt(v2).getTag();
                if (!(v0 instanceof b)) {
                    v0 = v1;
                }

                if (v0 != null && ((b) v0).a != null && ((b) v0).a.mUri != null) {
                    ((b) v0).mFullImage.setImageBitmap$1fdc9e65(v1);
                }
            }
        }
    }

    public static final class b {
        public final n h;
        ImageInfo a;
        String b;
        ProgressBar mProgress;
        TextView mProgressText;
        View mRetry;
        ImageViewTouch mFullImage;
        ImageView mThumbImage;

        b(n arg1) {
            this.h = arg1;

        }
    }

    final class FullPagerAdapter extends PagerAdapter {
        private final n d;
        private LinkedList mLinkList;
        private List mDatas;
        private LayoutInflater mLayoutInflater;

        public FullPagerAdapter(n arg2, Context arg3) {
            this.d = arg2;
            this.mLinkList = new LinkedList();
            this.mDatas = new ArrayList();
            this.mLayoutInflater = LayoutInflater.from(arg3);
        }

        public final int getCount() {
            return this.mDatas.size();
        }

        final void a(String arg7, Object arg8) {
            b v0_1 = null;
            b v1 = null;
            if (this.d.mImagePager == null || (android.support.design.a.isTextEmpty(arg7))) {
                v0_1 = v1;
            } else {
                int v4 = this.d.mImagePager.getChildCount();
                int v2 = 0;
                Object v3;
                Object v0;
                for (v3 = v1; v2 < v4; v3 = v0) {
                    v0 = this.d.mImagePager.getChildAt(v2).getTag();
                    if (!(v0 instanceof b)) {
                        v0 = v3;
                    }

                    if (v0 != null && ((b) v0).a != null && (arg7.equals(((b) v0).a.mUri))) {
                        this.a(v0_1, arg8);
                        return;
                    }
                    ++v2;
                }

                v0_1 = v1;
            }
            this.a(v0_1, arg8);
        }

        final void a(List arg2) {
            this.mDatas.clear();
            if (arg2 != null) {
                this.mDatas.addAll(arg2);
            }
        }

        private void a(b arg5, Object arg6) {
            int v1 = View.GONE;
            if (arg5 != null) {
                arg5.mProgress.setVisibility(v1);
                arg5.mProgressText.setVisibility(v1);
                if (arg6 == null) {
                    arg5.mFullImage.setVisibility(v1);
                    isViewFromObject(this.d.mSaveTextView, false);
                    Toast.makeText(this.d.mContext, com.ss.android.article.browser.R.string.n2, Toast.LENGTH_SHORT).show();
                    Object v0 = ComponentsManifest.findComponent(com.ss.android.aa.class);
                    if (v0 != null) {
                        ((com.ss.android.aa) v0).a(this.d.mContext, "image", "fail");
                    }
                } else {
                    arg5.mFullImage.setVisibility(View.VISIBLE);
                    arg5.mThumbImage.setVisibility(v1);
                    isViewFromObject(this.d.mSaveTextView, true);
                    if ((arg6 instanceof Bitmap)) {
                        arg5.mFullImage.setImageBitmap(((Bitmap) arg6));
                    } else if ((arg6 instanceof GifDrawable)) {
                        arg5.mFullImage.setImageDrawable(((Drawable) arg6));
                    }
                }
            }
        }

        public final int getItemPosition(Object arg2) {
            return this.mDatas.isEmpty() ? -2 : super.getItemPosition(arg2);
        }

        public final Object instantiateItem(ViewGroup arg9, int arg10) {
            View v2 = null;
            b v1_1;
            int v7 = View.GONE;
            Object v1 = this.mLinkList.size() > 0 ? this.mLinkList.removeFirst() : null;
            if (v1 == null) {
                v1_1 = new b(this.d);
                v2 = this.mLayoutInflater.inflate(com.ss.android.article.browser.R.layout.full_image_page, arg9, false);
                v2.setOnClickListener(v1_1.h.mOnClickListener);
                v1_1.mProgress = v2.findViewById(com.ss.android.article.browser.R.id.progress);
                v1_1.mProgressText = v2.findViewById(com.ss.android.article.browser.R.id.progress_text);
                v1_1.mRetry = v2.findViewById(com.ss.android.article.browser.R.id.retry);
                UIUtils.setVisibility(v1_1.mRetry, v7);
                v1_1.mFullImage = v2.findViewById(com.ss.android.article.browser.R.id.full_image);
                v1_1.mThumbImage = v2.findViewById(com.ss.android.article.browser.R.id.thumb_image);
                v1_1.mFullImage.setFitToWidth(true);
                ViewCompat.setLayerType(v1_1.mFullImage, ViewCompat.LAYER_TYPE_SOFTWARE, null);
                v1_1.mProgress.setVisibility(v7);
                v1_1.mFullImage.setMyOnClickListener(v1_1.h.mOnClickListener);
                v1_1.mFullImage.setDoubleTapListener(new com.ss.android.common.imagezoom.ImageViewTouch.b(v1_1));
                v2.setTag(v1_1);
            } else {
                v1_1 = (com.ss.android.image.n.b) ((View) v1).getTag();
            }

            Object v0 = this.d.g.get(arg10);
            v1_1.a = ((ImageInfo) v0);
            v1_1.b = null;
            if (this.d.h.size() >= arg10 + 1) {
                this.d.h.get(arg10);
            }

            if (((ImageInfo) v0).mKey != null) {
                v1_1.b = BaseImageManager.getCacheFileName(((ImageInfo) v0).mKey);
            }

            v1_1.mFullImage.setVisibility(v7);
            if (v1_1.a == null || v1_1.a.mUri == null) {
                v1_1.mProgress.setVisibility(v7);
            } else {
                v1_1.mProgress.setVisibility(View.VISIBLE);
                v1_1.mProgress.setProgress(0);
                v1_1.mProgressText.setVisibility(View.VISIBLE);
                v1_1.mProgressText.setText("");
                isViewFromObject(this.d.mSaveTextView, false);
                v0 = this.d.c != null ? this.d.c.get(v1_1.a.mUri) : null;
                if (v0 != null) {
                    this.a(v1_1, v0);
                    arg9.addView(v2);
                    return v2;
                }

                if (this.d.b == null) {
                    this.a(v1_1.a.mUri, null);
                    arg9.addView(v2);
                    return v2;
                }

                UIUtils.setVisibility(v1_1.mThumbImage, v7);
                LargeImageLoader v0_1 = this.d.b;
                String v4 = v1_1.a.mUri;
                String v1_2 = v1_1.a.mUrlList;
                if (!v0_1.c) {
                    arg9.addView(v2);
                    return v2;
                }

                String v5 = com.bytedance.common.utility.aa.md5(v4);
                if (v5 == null) {
                    v0_1.b.a(v4, null);
                    arg9.addView(v2);
                    return v2;
                }

                v0_1.a.addTask(v5, v4, v1_2, null);
            }

//            label_79:
            arg9.addView(v2);
            return v2;
        }

        public final void destroyItem(ViewGroup arg4, int arg5, Object arg6) {
            ImageInfo v1 = null;
            if (arg6 != null) {
                arg4.removeView(((View) arg6));
                this.mLinkList.addFirst(arg6);
                if (arg6 != null) {
                    Object v0 = ((View) arg6).getTag();
                    if (!(v0 instanceof b)) {
                        v0 = v1;
                    }

                    if (v0 == null) {
                        return;
                    }

                    ((b) v0).a = v1;
                    ((b) v0).b = null;
                    ((b) v0).mFullImage.setImageBitmap$1fdc9e65(null);
                }
            }
        }

        public final boolean isViewFromObject(View arg2, Object arg3) {
            boolean v0 = arg2 == (((View) arg3)) ? true : false;
            return v0;
        }

        public final ImageInfo b(int arg2) {
            Object v0;
            if (arg2 < 0 || arg2 >= this.mDatas.size()) {
                v0 = null;
            } else {
                v0 = this.mDatas.get(arg2);
            }

            return ((ImageInfo) v0);
        }
    }
}

